// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

#include "Import/BDFImport/BulkData/Property/PBEAMParser.h"

#include "gtest/gtest.h"

#include "DataStructure/Common/Constant.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Property/IPBEAMService.h"
using SG::DataStructure::Common::TOLERANCE;
using namespace SG::DataStructure::FEM;
#include "Import/BDFImport/BDFParserBase.h"
using namespace SG::DBManager;
using namespace SG::Import;
extern std::shared_ptr<DBServiceFactory> DB_SERVICEFACTORY;
extern std::vector<std::string>          dividedEntryInfo;

class PBEAMParserTest : public ::testing::Test
{
  public:
    static void SetUpTestSuite ()
    {
        DB_SERVICEFACTORY->ClearDatabase ();
        std::vector<std::string> Entry10 = { "PBEAM         10      21  12.000  56.000  17.000           3.930",
                                             "          -3.000    .867  -3.000   4.867   3.000   4.867   3.000    .867",
                                             "        YES         .100  10.830  45.612  13.847           3.201",
                                             "          -2.850    .824  -2.850   4.624   2.850   4.624   2.850    .824",
                                             "        YES         .200   9.720  36.742  11.154           2.579",
                                             "          -2.700    .780  -2.700   4.380   2.700   4.380   2.700    .780",
                                             "        YES         .300   8.670  29.232   8.874           2.052",
                                             "          -2.550    .737  -2.550   4.137   2.550   4.137   2.550    .737",
                                             "        YES         .400   7.680  22.938   6.963           1.610",
                                             "          -2.400    .694  -2.400   3.894   2.400   3.894   2.400    .694",
                                             "        YES         .500   6.750  17.719   5.379           1.244",
                                             "          -2.250    .650  -2.250   3.650   2.250   3.650   2.250    .650",
                                             "        YES         .600   5.880  13.446   4.082            .944",
                                             "          -2.100    .607  -2.100   3.407   2.100   3.407   2.100    .607",
                                             "        YES         .700   5.070   9.996   3.035            .702",
                                             "          -1.950    .564  -1.950   3.164   1.950   3.164   1.950    .564",
                                             "        YES         .800   4.320   7.258   2.203            .509",
                                             "          -1.800    .520  -1.800   2.920   1.800   2.920   1.800    .520",
                                             "        YES         .900   3.630   5.124   1.556            .360",
                                             "          -1.650    .477  -1.650   2.677   1.650   2.677   1.650    .477",
                                             "        YES        1.000   3.000   3.500   1.062            .246",
                                             "          -1.500    .434  -1.500   2.434   1.500   2.434   1.500    .434",
                                             "            .241           -.666      0.           70.43   1.10",
                                             "                                              0.   2.367      0.   1.184" };
        BDF::divideBulkEntry (Entry10, dividedEntryInfo);
        SG::Import::BDF::BULKDATA::ParsePBEAM (DB_SERVICEFACTORY, dividedEntryInfo);

        std::vector<std::string> Entry0 = { "PBEAM         11       2      6.      2.     4.5     1.2     6.5        +0000017",
                                            "+0000017    1.75      1.   -1.75      1.   -1.75     -1.    1.75     -1.+0000018",
                                            "+0000018    YESA      1.                                                +0000019",
                                            "+0000019      2.      1." };
        BDF::divideBulkEntry (Entry0, dividedEntryInfo);
        SG::Import::BDF::BULKDATA::ParsePBEAM (DB_SERVICEFACTORY, dividedEntryInfo);

        std::vector<std::string> Entry1 = { "PBEAM         12      21  12.000  56.000  17.000           3.930",
                                            "          -3.000    .867  -3.000   4.867   3.000   4.867   3.000    .867",
                                            "        YES        1.000   3.000   3.500   1.062            .246",
                                            "          -1.500    .434  -1.500   2.434   1.500   2.434   1.500    .434",
                                            "            .241           -.666      0.           70.43   1.10",
                                            "                                              0.   2.367      0.   1.184" };
        BDF::divideBulkEntry (Entry1, dividedEntryInfo);
        SG::Import::BDF::BULKDATA::ParsePBEAM (DB_SERVICEFACTORY, dividedEntryInfo);

        std::vector<std::string> Entry4 = { "PBEAM          2       2      7.7.1458332.333333        6.003188        +",
                                            "+           1.75      1.   -1.75      1.   -1.75     -1.    1.75     -1.+",
                                            "+             0.      0." };
        BDF::divideBulkEntry (Entry4, dividedEntryInfo);
        SG::Import::BDF::BULKDATA::ParsePBEAM (DB_SERVICEFACTORY, dividedEntryInfo);
    }
};

TEST_F (PBEAMParserTest, ParseEntrySeg10)
{
    // dbManager->Initialize ();

    // 测试数据
    SG::DataStructure::FEM::PBEAMData data;
    DB_SERVICEFACTORY->get<IPBEAMService> ()->FindById (10, data);

    ASSERT_TRUE (data.m_id == 10);
    ASSERT_TRUE (data.m_ccf == 0);
    ASSERT_TRUE (data.m_nsges == 10);
}

TEST_F (PBEAMParserTest, ParseEntrySeg0)
{
    // dbManager->Initialize ();

    // 测试数据
    SG::DataStructure::FEM::PBEAMData data;
    DB_SERVICEFACTORY->get<IPBEAMService> ()->FindById (11, data);

    ASSERT_TRUE (data.m_id == 11);
    ASSERT_TRUE (data.m_ccf == 1);
    ASSERT_TRUE (data.m_nsges == 1);
    ASSERT_NEAR (data.m_SO[0], 1.0, TOLERANCE);
    ASSERT_NEAR (data.m_SO[1], 1.0, TOLERANCE);
    ASSERT_NEAR (data.m_SO[10], 1.0, TOLERANCE);
}

TEST_F (PBEAMParserTest, ParseEntrySeg1)
{
    // dbManager->Initialize ();

    // 测试数据
    SG::DataStructure::FEM::PBEAMData data;
    DB_SERVICEFACTORY->get<IPBEAMService> ()->FindById (12, data);

    ASSERT_TRUE (data.m_id == 12);
    ASSERT_TRUE (data.m_ccf == 2);
    ASSERT_TRUE (data.m_nsges == 1);
}

TEST_F (PBEAMParserTest, ParseEntrySeg3)
{
    // dbManager->Initialize ();

    // 测试数据
    SG::DataStructure::FEM::PBEAMData data;
    DB_SERVICEFACTORY->get<IPBEAMService> ()->FindById (2, data);

    ASSERT_TRUE (data.m_id == 2);
    ASSERT_TRUE (data.m_ccf == 1);
    ASSERT_TRUE (data.m_nsges == 1);
}
